package vip.xiaonuo.invoice.modular.vo;

import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import vip.xiaonuo.invoice.core.enums.invoice.InvoicePlatformEnum;
import vip.xiaonuo.invoice.core.enums.invoice.InvoiceStatusEnum;
import vip.xiaonuo.invoice.core.adapter.dto.result.InvoiceResultDtoOfNuoNuo;
import vip.xiaonuo.invoice.core.adapter.dto.result.InvoiceResultDtoOfShuZU;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 统一发票详情VO
 * 用于统一不同平台的发票查询结果输出格式
 *
 * @author AI Assistant
 */
@Data
public class InvoiceDetailVo {
    /**
     * 发票ID
     */
    @JSONField(ordinal = 1)
    @Schema(description = "发票跟踪ID")
    private String invoicedTraceId;

    /**
     * 发票类型代码
     */
    @JSONField(ordinal = 2)
    @Schema(description = "发票类型代码")
    private String invoiceTypeCode;

    /**
     * 开票平台编码
     */
    @JSONField(ordinal = 3)
    @Schema(description = "开票平台")
    private String platform;

    /**
     * 购方名称
     */
    @JSONField(ordinal = 4)
    @Schema(description = "购方名称")
    private String buyerName;

    /**
     * 销方名称
     */
    @JSONField(ordinal = 5)
    @Schema(description = "销方名称")
    private String sellerName;

    /**
     * 销方税号
     */
    @JSONField(ordinal = 6)
    @Schema(description = "销方税号")
    private String sellerTaxNumber;

    /**
     * 开票人
     */
    @JSONField(ordinal = 7)
    @Schema(description = "开票人")
    private String issuer;

    /**
     * 发票状态码
     */
    @JSONField(ordinal = 8)
    @Schema(description = "发票状态")
    private String invoiceStatus;


    /**
     * 描述
     */
    @JSONField(ordinal = 8)
    @Schema(description = "描述")
    private String description;

    /**
     * 开票日期
     */
    @JSONField(ordinal = 9)
    @Schema(description = "开票日期")
    private String invoiceDate;

    /**
     * 合计金额(不含税)
     */
    @JSONField(ordinal = 10)
    @Schema(description = "合计金额(不含税)")
    private BigDecimal totalAmountWithoutTax;

    /**
     * 合计税额
     */
    @JSONField(ordinal = 11)
    @Schema(description = "合计税额")
    private BigDecimal totalTaxAmount;

    /**
     * 价税合计
     */
    @JSONField(ordinal = 12)
    @Schema(description = "价税合计")
    private BigDecimal totalAmountWithTax;

    /**
     * PDF文件URL
     */
    @JSONField(ordinal = 13)
    @Schema(description = "PDF文件URL")
    private String pdfUrl;

    /**
     * 商品明细列表
     */
    @JSONField(ordinal = 14)
    @Schema(description = "商品明细列表")
    private List<InvoiceItemVo> itemList = new ArrayList<>();

    /**
     * 商品明细VO
     */
    @Data
    public static class InvoiceItemVo {
        /**
         * 商品名称
         */
        @JSONField(ordinal = 1)
        @Schema(description = "商品名称")
        private String productName;

        /**
         * 金额
         */
        @JSONField(ordinal = 2)
        @Schema(description = "金额")
        private BigDecimal amount;

        /**
         * 税额
         */
        @JSONField(ordinal = 3)
        @Schema(description = "税额")
        private BigDecimal taxAmount;

        /**
         * 税率
         */
        @JSONField(ordinal = 4)
        @Schema(description = "税率")
        private BigDecimal taxRate;

        /**
         * 数量
         */
        @JSONField(ordinal = 5)
        @Schema(description = "数量")
        private BigDecimal quantity;

        /**
         * 单价
         */
        @JSONField(ordinal = 6)
        @Schema(description = "单价")
        private BigDecimal unitPrice;

        /**
         * 单位
         */
        @JSONField(ordinal = 7)
        @Schema(description = "单位")
        private String unit;

        /**
         * 规格型号
         */
        @JSONField(ordinal = 8)
        @Schema(description = "规格型号")
        private String specification;
    }

    /**
     * 从数族发票结果DTO转换为统一VO
     */
    public static InvoiceDetailVo fromShuZuDto(InvoiceResultDtoOfShuZU dto) {
        InvoiceDetailVo vo = new InvoiceDetailVo();

        // 设置发票详情
        if (dto.getContent() != null) {
            InvoiceResultDtoOfShuZU.content shuZuContent = dto.getContent();
            vo.setBuyerName(shuZuContent.getGfmc());
            vo.setSellerName(shuZuContent.getXfmc());
            vo.setSellerTaxNumber(shuZuContent.getXfsh());
            vo.setInvoiceTypeCode(shuZuContent.getFplxdm());
            vo.setIssuer(shuZuContent.getKpr());
            vo.setPdfUrl(shuZuContent.getPdfUrl());

            // 转换金额字段为BigDecimal
            if (shuZuContent.getHjje() != null) {
                vo.setTotalAmountWithoutTax(new BigDecimal(shuZuContent.getHjje()));
            }
            if (shuZuContent.getHjse() != null) {
                vo.setTotalTaxAmount(new BigDecimal(shuZuContent.getHjse()));
            }
            if (shuZuContent.getJshj() != null) {
                vo.setTotalAmountWithTax(new BigDecimal(shuZuContent.getJshj()));
            }

            // 转换商品明细
            if (shuZuContent.getMxList() != null && !shuZuContent.getMxList().isEmpty()) {
                List<InvoiceItemVo> itemList = new ArrayList<>();
                for (InvoiceResultDtoOfShuZU.MxList mx : shuZuContent.getMxList()) {
                    InvoiceItemVo itemVo = new InvoiceItemVo();
                    itemVo.setProductName(mx.getSpmc());
                    if (mx.getJe() != null) {
                        itemVo.setAmount(new BigDecimal(mx.getJe()));
                    }
                    if (mx.getSe() != null) {
                        itemVo.setTaxAmount(new BigDecimal(mx.getSe()));
                    }
                    if (mx.getSl() != null) {
                        itemVo.setTaxRate(new BigDecimal(mx.getSl()));
                    }
                    if (mx.getSpsl() != null) {
                        itemVo.setQuantity(new BigDecimal(mx.getSpsl()));
                    }
                    if (mx.getDj() != null) {
                        itemVo.setUnitPrice(new BigDecimal(mx.getDj()));
                    }
                    itemVo.setUnit(mx.getDw());
                    itemVo.setSpecification(mx.getGgxh());
                    itemList.add(itemVo);
                }
                vo.setItemList(itemList);
            }
        }

        return vo;
    }

    /**
     * 从诺诺发票结果DTO转换为统一VO
     */
    public static InvoiceDetailVo fromNuoNuoDto(InvoiceResultDtoOfNuoNuo dto, String invoicedTraceId, String platformCode, String invoiceStatus, String failReason) {
        InvoiceDetailVo vo = new InvoiceDetailVo();
        // 直接设置发票基本信息
        vo.setInvoicedTraceId(invoicedTraceId);
        vo.setPlatform(Objects.requireNonNull(InvoicePlatformEnum.getByCode(platformCode)).getName());
        vo.setInvoiceStatus(Objects.requireNonNull(InvoiceStatusEnum.getByCode(invoiceStatus)).getName());

        // 设置发票详情
        if (dto.getContent() != null) {
            InvoiceResultDtoOfNuoNuo.content nuoNuoContent = dto.getContent();
            vo.setBuyerName(nuoNuoContent.getGfmc());
            vo.setSellerName(nuoNuoContent.getXfmc());
            vo.setSellerTaxNumber(nuoNuoContent.getXfsh());
            vo.setInvoiceTypeCode(nuoNuoContent.getFplxdm());
            vo.setInvoiceDate(nuoNuoContent.getKprq());
            vo.setIssuer(nuoNuoContent.getKpr());
            vo.setPdfUrl(nuoNuoContent.getPdfUrl());

            // 转换金额字段为BigDecimal
            if (nuoNuoContent.getHjje() != null) {
                vo.setTotalAmountWithoutTax(new BigDecimal(nuoNuoContent.getHjje()));
            }
            if (nuoNuoContent.getHjse() != null) {
                vo.setTotalTaxAmount(new BigDecimal(nuoNuoContent.getHjse()));
            }
            if (nuoNuoContent.getJshj() != null) {
                vo.setTotalAmountWithTax(new BigDecimal(nuoNuoContent.getJshj()));
            }

            // 转换商品明细
            if (nuoNuoContent.getMxList() != null) {
                List<InvoiceItemVo> itemList = new ArrayList<>();
                for (InvoiceResultDtoOfNuoNuo.MxList mx : nuoNuoContent.getMxList()) {
                    InvoiceItemVo itemVo = new InvoiceItemVo();
                    itemVo.setProductName(mx.getSpmc());
                    if (mx.getJe() != null) {
                        itemVo.setAmount(new BigDecimal(mx.getJe()));
                    }
                    if (mx.getSe() != null) {
                        itemVo.setTaxAmount(new BigDecimal(mx.getSe()));
                    }
                    if (mx.getSl() != null) {
                        itemVo.setTaxRate(new BigDecimal(mx.getSl()));
                    }
                    if (mx.getSpsl() != null) {
                        itemVo.setQuantity(new BigDecimal(mx.getSpsl()));
                    }
                    if (mx.getDj() != null) {
                        itemVo.setUnitPrice(new BigDecimal(mx.getDj()));
                    }
                    itemVo.setUnit(mx.getDw());
                    itemVo.setSpecification(mx.getGgxh());
                    itemList.add(itemVo);
                }
                vo.setItemList(itemList);
            }
        }

        return vo;
    }
}
